import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'package:quintesson/state/login_state.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../apis.dart';
import '../const.dart';
import '../routes.dart';
import '../utils/mydio.dart';
import '../utils/utils.dart';
import '../view/textfield.dart';

class Login extends StatefulWidget {
  final String pageIndex;

  Login({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  _LoginState createState() => _LoginState(pageIndex: pageIndex);
}

class _LoginState extends State<Login> {
  String pageIndex;
  Map data = Map<String, String>();
  var _scaffoldkey = new GlobalKey<ScaffoldState>();
  SharedPreferences _preferences;
  static final String NAME = 'name';
  static final String PASS = 'pass';

  _LoginState({String pageIndex}) : this.pageIndex = pageIndex;

  @override
  void initState() {
    _getSp();
  }

  _getSp() async {
    _preferences = await SharedPreferences.getInstance();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      key: _scaffoldkey,
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.fromLTRB(33, 30, 33, 0),
            child: Column(
              children: <Widget>[
                EditText(
                  keyboardType: TextInputType.number,
                  data: data,
                  name: NAME,
                  labelText: '请输入手机号',
                  value: '',
                ),
                PassField(
                  labelText: '密码',
                  map: data,
                  mapKey: PASS,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    GestureDetector(
                      onTap: () {
                        String route = '${Routes.forget_pwd}';
                        Routes.router.navigateTo(context, route,
                            replace: false, transition: TransitionType.fadeIn);
                      },
                      child: Text(
                        '忘记密码',
                        style: TextStyle(fontSize: 16, color: Const.gold),
                      ),
                    )
                  ],
                ),
              ],
            ),
          ),
          MaterialButton(
            height: 68,
            minWidth: MediaQuery.of(context).size.width,
            elevation: 0,
            onPressed: () {
              if (data[NAME] == null) {
                Utils.getInstance().showSnackBar(_scaffoldkey, '用户名不能为空');
                return;
              }
              if (data[PASS] == null) {
                Utils.getInstance().showSnackBar(_scaffoldkey, '密码不能为空');
                return;
              }

              // 登录
              MyDio.getInstance().post(Apis.login, (data) async {
                Utils.getInstance().showSnackBar(_scaffoldkey, data['msg']);
                if (data['state'] == '0') {
                  Provider.of<LoginState>(context, listen: false)
                      .setLoginState(Const.BIND);
                  _preferences.setString(Const.UUID, '123456');
                  Navigator.pop(context);
                }
              }, params: data);
            },
            child: Text(
              "登    录",
              style: TextStyle(color: Colors.white, fontSize: 20),
            ),
            color: Const.gold,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(30),
                    bottomRight: Radius.circular(30))),
          ),
        ],
      ),
    );
  }
}
